package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysGoodsComment;
import com.ruoyi.project.system.domain.SysGoodsImg;
import com.ruoyi.project.system.service.ISysGoodsCommentService;
import com.ruoyi.project.system.service.ISysGoodsImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mobileUser/goodsComment/")
public class UserGoodsCommentController extends BaseController {

@Autowired
private ISysGoodsCommentService sysGoodsCommentService;

@Autowired
private ISysGoodsImgService sysGoodsImgService;


    /**
     * 用户新增二手物品评论
     * 登录之后做评论
     */
    @RepeatSubmit
    @SignatureValidation
    @PostMapping("addGoodsComment")
    public AjaxResult add(@RequestBody SysGoodsComment sysGoodsComment)
    {
        List<String> urls = new ArrayList<String>();
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        sysGoodsComment.setMobileUserId(mobileUserId);
        sysGoodsCommentService.insertSysGoodsComment(sysGoodsComment);
        SysGoodsImg sysGoodsImg = new SysGoodsImg();
        urls = sysGoodsComment.getUrls();
        if( null==urls){
            return AjaxResult.success();
        }
        sysGoodsImg.setCommentId(sysGoodsComment.getCommentId());
        for (int i = 0; i < urls.size(); i++) {
            sysGoodsImg.setUrl(urls.get(i));
            sysGoodsImgService.insertSysGoodsImg(sysGoodsImg);
        }
      return AjaxResult.success();
    }

    /**
     * 查询二手物品的详细评论
     * */
    @SignatureValidation
    @GetMapping("queryComments")
    public AjaxResult list(SysGoodsComment sysGoodsComment)
    {
        startPage();
        SysGoodsImg sysGoodsImg = new SysGoodsImg();
        sysGoodsImg.setCommentId(sysGoodsComment.getCommentId());
        Long commentId = sysGoodsComment.getCommentId();
        List<SysGoodsComment> sysGoodsComments = sysGoodsCommentService.selectSysGoodsCommentLists(sysGoodsComment);
        if(sysGoodsComment.getCommentId() == null){
            return AjaxResult.success(sysGoodsComments);
        }
        List<SysGoodsComment> sysGoodsAllComment = sysGoodsCommentService.queryAllParent(sysGoodsComment);
        List<SysGoodsImg> sysGoodsImgs = sysGoodsImgService.selectSysGoodsImgList(sysGoodsImg);
        Map map = new HashMap();
        map.put("sysGoodsImgs",sysGoodsImgs);
        map.put("sysGoodsComments",sysGoodsComments);
        map.put("sysGoodsAllComment",sysGoodsAllComment);
        return AjaxResult.success(map);
    }

    /**
     * 用户端回复物品评论
     * 传 commentID = parentCommentId
     * */
    @SignatureValidation
    @RepeatSubmit
    @PostMapping("replyComments")
    public AjaxResult replyComments(@RequestBody SysGoodsComment sysGoodsComment){
        List<String> urls = new ArrayList<String>();
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        sysGoodsComment.setMobileUserId(mobileUserId);
        sysGoodsComment.setParentCommentId(sysGoodsComment.getCommentId());
        sysGoodsCommentService.insertSysGoodsComment(sysGoodsComment);
        SysGoodsImg sysGoodsImg = new SysGoodsImg();
        urls = sysGoodsComment.getUrls();

        if(null == urls){
            return AjaxResult.success();
        }

        sysGoodsImg.setCommentId(sysGoodsComment.getCommentId());
        for (int i = 0; i < urls.size(); i++) {
            sysGoodsImg.setUrl(urls.get(i));
            sysGoodsImgService.insertSysGoodsImg(sysGoodsImg);
        }
        return AjaxResult.success();
    }

    /**
     * 用户删除自己的评论
     * 连带评论图片
     * 传commentId   删除评论
     * */
    @PostMapping("deleteComent")
    public AjaxResult deleteMoment(@RequestBody SysGoodsComment sysGoodsComment){
        Long commentId = sysGoodsComment.getCommentId();
        sysGoodsCommentService.deleteSysGoodsCommentById(commentId);
        sysGoodsImgService.deleteSysGoodsImgByCommentId(commentId);
        sysGoodsCommentService.deleteSysGoodsCommentByParentId(commentId);
        return AjaxResult.success();
    }
}
